<template>
  <view class="container">
    <tui-tabs :tabs="tabs" :isFixed="scrollTop>=0" :currentTab="currentTab" selectedColor="#E41F19" sliderBgColor="#E41F19"
      @change="change"></tui-tabs>
    <!--选项卡逻辑自己实现即可，此处未做处理-->
    <view :class="{'tui-order-list':scrollTop>=0}">
      <view class="tui-order-item" v-for="(item,index) of orderList" :key="index">
        <tui-list-cell :hover="false" :lineLeft="false">
          <view class="tui-goods-title">
            <view>订单号：{{item.lsh}}</view>
            <view class="tui-order-status">{{item.zt | formatZt}}</view>
          </view>
        </tui-list-cell>
        <block>
          <tui-list-cell padding="0" @tap="detail(item.lsh)" v-for="(orderDetail,index1) of item.orderDetail" :key="index1">
            <view class="tui-goods-item">
              <image :src="orderDetail.goodsimg" class="tui-goods-img"></image>
              <view class="tui-goods-center">
                <view class="tui-goods-name">{{orderDetail.title}}</view>
                <view class="tui-goods-attr">{{orderDetail.skus}}</view>
              </view>
              <view class="tui-price-right">
                <view class="line-del">￥{{orderDetail.price}}</view>
                <view class="text-red text-bold">￥{{orderDetail.money}}</view>
                <view>x{{orderDetail.count}}</view>
              </view>
            </view>
          </tui-list-cell>
        </block>
        <tui-list-cell :hover="false" :last="true">
          <view class="tui-goods-price">
            <view v-if="item.orderDetail">共{{item.orderDetail.length}}件商品 合计：</view>
            <view class="tui-size-24">￥</view>
            <view class="tui-price-large">{{(item.money+"").split(".")[0]}}</view>
            <view class="tui-size-24">.{{(item.money+"").split(".")[1]}}</view>
          </view>
        </tui-list-cell>
        <view class="tui-order-btn">
          <view class="tui-btn-ml" v-if="item.zt && item.zt!=0">
            <tui-button type="black" :plain="true" width="148rpx" height="56rpx" :size="26" shape="circle">查看发票</tui-button>
          </view>
          <view class="tui-btn-ml" v-if="item.zt && item.zt==0" @tap="detail(item.lsh)">
            <tui-button type="danger" :plain="false" width="148rpx" height="56rpx" :size="26" shape="circle">去付款</tui-button>
          </view>
          <view class="tui-btn-ml" v-if="item.zt && item.zt==1">
            <tui-button type="green" :plain="false" width="148rpx" height="56rpx" :size="26" shape="circle">提醒发货</tui-button>
          </view>
          <view class="tui-btn-ml" v-if="item.zt && item.zt==2">
            <tui-button type="blue" :plain="false" width="148rpx" height="56rpx" :size="26" shape="circle">确认收货</tui-button>
          </view>
          <view class="tui-btn-ml" v-if="item.zt && item.zt==3">
            <tui-button type="warning" :plain="false" width="148rpx" height="56rpx" :size="26" shape="circle">评价晒单</tui-button>
          </view>
          <view class="tui-btn-ml">
            <tui-button type="danger" :plain="true" width="148rpx" height="56rpx" :size="26" shape="circle" v-if="item.zt && item.zt!=0">再次购买</tui-button>
          </view>
        </view>
      </view>

    </view>
    <!--加载loadding-->
    <tui-loadmore :visible="loadding" :index="3" type="red"></tui-loadmore>
    <tui-nomore :visible="!pullUpOn" bgcolor="#fafafa"></tui-nomore>
    <!--加载loadding-->

  </view>
</template>

<script>
  import tuiTabs from "@/components/tui-tabs/tui-tabs"
  import tuiButton from "@/components/extend/button/button"
  import tuiLoadmore from "@/components/loadmore/loadmore"
  import tuiNomore from "@/components/nomore/nomore"
  import tuiListCell from "@/components/list-cell/list-cell"
  import tuiSticky from "@/components/sticky/sticky"
  export default {
    components: {
      tuiTabs,
      tuiButton,
      tuiLoadmore,
      tuiNomore,
      tuiListCell,
      tuiSticky
    },
    data() {
      return {
        tabs: [{
          name: "全部"
        }, {
          name: "待付款"
        }, {
          name: "待发货"
        }, {
          name: "待收货"
        }, {
          name: "待评价"
        }],
        currentTab: 0,
        pageIndex: 1,
        loadding: false,
        pullUpOn: true,
        scrollTop: 0,
        orderList: [{
          "id": 0,
          "lsh": "无",
          "userid": 0,
          "storeid": 0,
          "price": 0,
          "money": 0,
          "reduces": "",
          "zt": "0",
          "orderDetail": []
        }],
        page: 1,
      }
    },
    methods: {
      change(e) {
        this.currentTab = parseInt(e.index)
        this.page = 1;
        this.getOrdersList();
      },
      detail(lsh) {
        uni.navigateTo({
          url: '../orderDetail/orderDetail?lsh='+lsh
        })
      },
      //查询商品列表
      getOrdersList() {
        let that = this;
        this.$tui.request('orders/getOrders/' + this.page + '/10', {
            userid: this.$store.state.userId,
            type: this.currentTab == 0 ? '' : this.currentTab - 1
          }, "get", true)
          .then(res => {
            if (res.code == "200") {
              if (that.page == 1) {
                that.orderList = res.data
              } else {
                that.orderList.push.apply(that.orderList, res.data);
              }
            } else {
              if (that.page == 1) {
                that.orderList = []
              }
            }
          })
          .catch(res => {
            if (that.page == 1) {
              that.orderList = []
            }
          })
      }
    },
    onLoad(e) {
      if (e.id) {
        this.currentTab = parseInt(e.id)
        this.getOrdersList();
      }
    },
    onPullDownRefresh() {
      this.page = 1;
      this.getOrdersList();
      setTimeout(() => {
        uni.stopPullDownRefresh()
      }, 200);
    },
    onReachBottom() {
      //只是测试效果，逻辑以实际数据为准
      this.loadding = true
      this.pullUpOn = true
      this.page++;
      this.getOrdersList();
      setTimeout(() => {
        this.loadding = false
        this.pullUpOn = false
      }, 1000)
    },
    onPageScroll(e) {
      this.scrollTop = e.scrollTop;
    },
    filters: {
      formatZt: function(zt) {
        let _s = "";
        switch (parseInt(zt)) {
          case 0:
            _s = "待付款";
            break;
          case 1:
            _s = "待发货";
            break;
          case 2:
            _s = "待收货";
            break;
          case 3:
            _s = "待评价";
            break;
          case 4:
            _s = "售后中";
            break;
          case 5:
            _s = "已取消";
            break;
          case 6:
            _s = "已完成";
            break;
        }
        return _s;
      }
    }
  }
</script>

<style>
  page{
    background-color: #FFFFFF;
  }
  .container {
    padding-bottom: env(safe-area-inset-bottom);
  }

  .tui-order-list {
    margin-top: 80rpx;
  }

  .tui-order-item {
    margin-top: 20rpx;
    border-radius: 10rpx;
    overflow: hidden;
  }

  .tui-goods-title {
    width: 100%;
    font-size: 28rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .tui-order-status {
    color: #888;
    font-size: 26rpx;
  }

  .tui-goods-item {
    width: 100%;
    padding: 20rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
  }

  .tui-goods-img {
    width: 180rpx;
    height: 180rpx;
    display: block;
    flex-shrink: 0;
  }

  .tui-goods-center {
    flex: 1;
    padding: 20rpx 8rpx;
    box-sizing: border-box;
  }

  .tui-goods-name {
    max-width: 310rpx;
    word-break: break-all;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    font-size: 26rpx;
    line-height: 32rpx;
  }

  .tui-goods-attr {
    font-size: 22rpx;
    color: #888888;
    line-height: 32rpx;
    padding-top: 20rpx;
    word-break: break-all;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .tui-price-right {
    text-align: right;
    font-size: 24rpx;
    color: #888888;
    line-height: 30rpx;
    padding-top: 20rpx;
  }

  .tui-color-red {
    color: #E41F19;
    padding-right: 30rpx;
  }

  .tui-goods-price {
    width: 100%;
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    font-size: 24rpx;
  }

  .tui-size-24 {
    font-size: 24rpx;
    line-height: 24rpx;
  }

  .tui-price-large {
    font-size: 32rpx;
    line-height: 30rpx;
    font-weight: 500;
  }

  .tui-order-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    background: #fff;
    padding: 10rpx 30rpx 20rpx;
    box-sizing: border-box;
  }

  .tui-btn-ml {
    margin-left: 20rpx;
  }
</style>
